package com.css.fxfzdzzh.modules.eqLandslide.lawAnalysis.controller;

import com.alibaba.fastjson.JSON;
import com.css.fxfzdzzh.base.response.RestResponse;
import com.css.fxfzdzzh.modules.drill.service.DrillService;
import com.css.fxfzdzzh.modules.eqLandslide.lawAnalysis.param.CountParam;
import com.css.fxfzdzzh.modules.eqLandslide.lawAnalysis.service.LawAnalysisService;
import com.css.fxfzdzzh.modules.evaluate.VO.VersionPageVO;
import com.css.fxfzdzzh.modules.hypergraph.service.HypergraphService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Description: fxfzdzzh
 * @Author: lhl
 * @CreateDate: 2023/2/27 14:55
 */
@RestController
@RequestMapping("/lawAnalysis")
public class LawAnalysisController {

    @Resource
    LawAnalysisService lawAnalysisService;
    @Resource
    DrillService drillService;

    @Value("${basicData.url}")
    public String basicDataUrl;

    @Resource
    HypergraphService hypergraphService;

    @GetMapping("/getJcEarthquakeDamageSurvey")
    public RestResponse getJcEarthquakeDamageSurvey() {
        RestResponse restResponse = null;
        try {
//            String url = basicDataUrl + "/datacenter/jcEarthquakeDamageSurvey/all";//全部历史地震数据
            //全部历史地震数据（包含等震线数据的）
            String url = basicDataUrl + "/datacenter/jcEarthquakeDamageSurvey/dzdzzhpg/listHasIsolineData";
            restResponse = drillService.getJCSJ(url, 3);
            restResponse.setMessage("获取历史地震等震线成功！");
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail("获取历史地震等震线失败！");
        }
        return restResponse;
    }

    @PostMapping("/count1")
    public RestResponse count1(@RequestBody CountParam param1) {
        RestResponse restResponse = null;
        try {
            restResponse = lawAnalysisService.count1(param1);
            if (restResponse.getCode() == 200) {
                //刷新指定超图工作空间数据源数据集
                boolean bool = hypergraphService.getWorkspaceReload("dzzh_slide_result_intensity");
                restResponse.setMessage("与地震烈度的关系计算成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail("与地震烈度的关系计算失败！");
        }
        return restResponse;
    }

    /**
     * @param map
     *          tab :   "1"://断层
     *                  "2"://高程
     *                  "3"://地层岩性
     *                  "4"://道路
     *                  "5"://地震动峰值加速度数据（50年超越概率2%）
     *                  "6"://地震动峰值加速度数据（50年超越概率10%）
     *                  "7"://地震动峰值加速度数据（50年超越概率63%）
     *            vo :  versionDesc;//版本说明
     *                  uploadUser;//上传人
     *                  uploadStartTime;//上传日期开始日期
     *                  uploadEndTime;//上传日期结束日期
     *                  page;
     *                  rows;
     *                  province;
     * @return
     */
    @PostMapping("/getParam1Version")
    public RestResponse getParam1Version(@RequestBody Map<String, Object> map) {
        RestResponse restResponse = null;
        try {
            VersionPageVO vo=JSON.parseObject(JSON.toJSONString(map.get("vo")),VersionPageVO.class);
            restResponse = lawAnalysisService.getParam1Version((String) map.get("tab"), vo);
            restResponse.setMessage("获取成功！");
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail("获取失败！");
        }
        return restResponse;
    }

    @PostMapping("/getParam2Version")
    public RestResponse getParam2Version(@RequestBody VersionPageVO vo) {
        RestResponse restResponse = null;
        try {
            vo.setTypeCode(null);
            restResponse = lawAnalysisService.getParam2Version(vo);
            restResponse.setMessage("获取成功！");
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail("获取失败！");
        }
        return restResponse;
    }

    @PostMapping("/getParam3Version")
    public RestResponse getParam3Version(@RequestBody VersionPageVO vo) {
        RestResponse restResponse = null;
        try {
            vo.setTypeCode(null);
            restResponse = lawAnalysisService.getParam3Version(vo);
            restResponse.setMessage("获取成功！");
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail("获取失败！");
        }
        return restResponse;
    }

    @PostMapping("/count2")
    public RestResponse count2(@RequestBody CountParam param1) {
        RestResponse restResponse = null;
        try {
            restResponse = lawAnalysisService.count2(param1);
            if (restResponse.getCode() == 200) {
                //刷新指定超图工作空间数据源数据集
                boolean bool = hypergraphService.getWorkspaceReload("dzzh_slide_result_fault");
                restResponse.setMessage("与距发震断层距离的关系计算成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail("与距发震断层距离的关系计算失败！");
        }
        return restResponse;
    }

    @PostMapping("/count3")
    public RestResponse count3(@RequestBody CountParam param1) {
        RestResponse restResponse = null;
        try {
            restResponse = lawAnalysisService.count3(param1);
            if (restResponse.getCode() == 200) {
                //刷新指定超图工作空间数据源数据集
                boolean bool = hypergraphService.getWorkspaceReload("dzzh_slide_result_tera");
                restResponse.setMessage("与高程的关系计算成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail("与高程的关系计算失败！");
        }
        return restResponse;
    }

    @PostMapping("/count4")
    public RestResponse count4(@RequestBody CountParam param1) {
        RestResponse restResponse = null;
        try {
            restResponse = lawAnalysisService.count4(param1);
            if (restResponse.getCode() == 200) {
                //刷新指定超图工作空间数据源数据集
                boolean bool = hypergraphService.getWorkspaceReload("dzzh_slide_result_slope");
                restResponse.setMessage("与地形坡度的关系计算成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail("与地形坡度的关系计算失败！");
        }
        return restResponse;
    }

    @PostMapping("/count5")
    public RestResponse count5(@RequestBody CountParam param1) {
        RestResponse restResponse = null;
        try {
            restResponse = lawAnalysisService.count5(param1);
            if (restResponse.getCode() == 200) {
                //刷新指定超图工作空间数据源数据集
                boolean bool = hypergraphService.getWorkspaceReload("dzzh_slide_result_aspect");
                restResponse.setMessage("与坡向的关系计算成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail("与坡向的关系计算失败！");
        }
        return restResponse;
    }

    @PostMapping("/count6")
    public RestResponse count6(@RequestBody CountParam param1) {
        RestResponse restResponse = null;
        try {
            restResponse = lawAnalysisService.count6(param1);
            if (restResponse.getCode() == 200) {
                //刷新指定超图工作空间数据源数据集
                boolean bool = hypergraphService.getWorkspaceReload("dzzh_slide_result_drainage");
                restResponse.setMessage("与水系的关系计算成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail("与水系的关系计算失败！");
        }
        return restResponse;
    }

    @PostMapping("/count7")
    public RestResponse count7(@RequestBody CountParam param1) {
        RestResponse restResponse = null;
        try {
            restResponse = lawAnalysisService.count7(param1);
            if (restResponse.getCode() == 200) {
                //刷新指定超图工作空间数据源数据集
                boolean bool = hypergraphService.getWorkspaceReload("dzzh_slide_result_lithology");
                restResponse.setMessage("与地层岩性的关系计算成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail("与地层岩性的关系计算失败！");
        }
        return restResponse;
    }

    /**
     * 查询参数及结果
     *
     * @param tab 1、与地震烈度2、与距发震断层距离3、与高程4、与地形坡度5、与坡向6、与水系7、与地层岩性
     * @return
     */
    @GetMapping("/getDataByType")
    public RestResponse getDataByType(@RequestParam String tab) {
        RestResponse restResponse = null;
        try {
            restResponse = lawAnalysisService.getDataByType(tab);
            restResponse.setMessage("查询成功！");
        } catch (Exception e) {
            e.printStackTrace();
            restResponse = RestResponse.fail("查询失败！");
        }
        return restResponse;
    }

    /**
     * 导出结果
     *
     * @param map dspId: DzzhSlideParam的id
     *            type:
     *            1、与地震烈度2、与距发震断层距离3、与高程4、与地形坡度5、与坡向6、与水系7、与地层岩性
     * @return
     */
    @PostMapping("/exportByDspIdAndType")
    public void exportByDspIdAndType(HttpServletResponse response, @RequestBody Map<String, String> map) {
        lawAnalysisService.exportByDspIdAndType(response, map.get("dspId"), map.get("type"));
    }
}
